package com.hsy.crm.web.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hsy.crm.web.domain.cm.App;
import com.hsy.crm.web.domain.cm.PageResult;
import com.hsy.crm.web.domain.cm.Unified;
import com.hsy.crm.web.domain.entity.Store;
import com.hsy.crm.web.domain.entity.StoreProduct;
import com.hsy.crm.web.domain.entity.TransferOrderDetail;
import com.hsy.crm.web.domain.po.store.StonePagePo;
import com.hsy.crm.web.domain.vo.store.StonePageVo;
import com.hsy.crm.web.domain.vo.store.StoreSelectVo;
import com.hsy.crm.web.mapper.StoneMapper;
import com.hsy.crm.web.mapper.StoreProductMapper;
import com.hsy.crm.web.mapper.TransferOrderDetailMapper;
import com.hsy.crm.web.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class StoneService extends BaseService<Store> {

    @Autowired
    StoneMapper stoneMapper;

    @Autowired
    StoreProductMapper storeProductMapper;

    @Autowired
    TransferOrderDetailMapper transferOrderDetailMapper;

    public Unified<List<Store>> select(StoreSelectVo store) {
        Unified<List<Store>> unified = new Unified<>();
        if( store.getTransfer()!=null && store.getTransfer() == 1 ){
            unified.setData(stoneMapper.selectTransferStore(store));
        }else{
            unified.setData(stoneMapper.selectStore(store));
        }

        return unified;
    }

    public Unified<Integer> deleteStore(Store store) {
        Unified<Integer> unified = new Unified<>();
        StoreProduct storeProduct = new StoreProduct();
        storeProduct.setStoreId(store.getId());
        storeProduct.setCustomerId(store.getCustomerId());
        if(storeProductMapper.checkStore(storeProduct)!=null){
            unified.setResult(App.BussError.STORE_DELETE_PRODUCT.getCode());
        }else{

            TransferOrderDetail detail = new TransferOrderDetail();
            detail.setCustomerId(store.getCustomerId());
            detail.setInStoreId(store.getId());
            int count = transferOrderDetailMapper.selectCount(detail);
            if( count > 0 ){
                unified.setResult(App.BussError.STORE_DELETE_TRANSFER.getCode());
                return unified;
            }

            detail = new TransferOrderDetail();
            detail.setCustomerId(store.getCustomerId());
            detail.setOutStoreId(store.getId());
            count = transferOrderDetailMapper.selectCount(detail);
            if( count > 0 ){
                unified.setResult(App.BussError.STORE_DELETE_TRANSFER.getCode());
                return unified;
            }


            this.delete(store);
        }
        return unified;
    }

    public PageResult<StonePagePo> page(StonePageVo stonePageVo) {
        Page page = PageHelper.startPage(stonePageVo.getPage(),stonePageVo.getLimit());
        PageResult<StonePagePo> pageResult = new PageResult<>();
        pageResult.setRows(stoneMapper.page(stonePageVo));
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

    public Unified<Store> getDefault(Store Store) {
        Unified<Store> unified = new Unified<>();

        Store entity = new Store();
        entity.setCustomerId(Store.getCustomerId());
        entity.setCompanyId(Store.getLoginCompanyId());
        entity.setDefaultStone(1);
        Store = stoneMapper.selectOne(entity);

        unified.setData(Store);
        return unified;
    }

    public Unified<Integer> setDefault(Store store) {
        String customerId = store.getCustomerId();
        Unified<Integer> unified = new Unified<>();

        store = stoneMapper.selectByPrimaryKey(store);
        store.setCustomerId(customerId);
        Store updateStore = new Store();
        updateStore.setDefaultStone(0);
        updateStore.setCustomerId(customerId);
        stoneMapper.updateByExampleSelective(updateStore,getCompanyExample(store));

        updateStore = new Store();
        updateStore.setDefaultStone(1);
        updateStore.setId(store.getId());
        updateStore.setCustomerId(customerId);
        stoneMapper.updateByPrimaryKeySelective(updateStore);

        return unified;
    }

    private Example getCompanyExample(Store store){
        Example example=new Example(Store.class);
        example.setTableName( store.getDynamicTableName() );
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("companyId", store.getCompanyId());
        return example;
    }
    
}
